﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsInfo : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsInfo()
        {
            InitializeComponent();
        }

        Operator op = new Operator();
        

        private void FrmDrugsInfo_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            try
            {
                DataTable dt = new DataTable();
                string sql = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo order by drugsId";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
                //this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//设置列标题的对齐方式为居中
                //this.dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//设置单元格对齐方式为居中
                //this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtBoxDrugsID.ReadOnly = true;
            this.txtBoxDrugsName.ReadOnly = false;
            this.txtBoxDrugsPY.ReadOnly = false;
            this.txtBoxDrugsSpec.ReadOnly = false;
            this.txtBoxDrugsRegNum.ReadOnly = false;
            this.txtBoxDrugsFactory.ReadOnly = false;
            this.txtBoxDrugsInPrice.ReadOnly = false;
            this.txtBoxDrugsSalePrice.ReadOnly = false;
            this.txtBoxDrugsID.Text = "";
            this.txtBoxDrugsName.Text = "";
            this.txtBoxDrugsPY.Text = "";
            this.txtBoxDrugsSpec.Text = "";
            this.comboDrugsUnit.Text = "";
            this.txtBoxDrugsRegNum.Text = "";
            this.txtBoxDrugsFactory.Text = "";
            this.txtBoxDrugsInPrice.Text = "";
            this.txtBoxDrugsSalePrice.Text = "";
            this.txtBoxDrugsName.Focus();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsInfo where drugsId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "users");
                this.txtBoxDrugsID.Text = dt_1.Rows[0]["drugsId"].ToString();
                this.txtBoxDrugsName.Text = dt_1.Rows[0]["drugsName"].ToString();
                this.txtBoxDrugsPY.Text = dt_1.Rows[0]["drugsPY"].ToString();
                this.txtBoxDrugsSpec.Text = dt_1.Rows[0]["drugsSpecification"].ToString();
                this.comboDrugsUnit.Text = dt_1.Rows[0]["drugsUnit"].ToString();
                this.txtBoxDrugsRegNum.Text = dt_1.Rows[0]["drugsRegNum"].ToString();
                this.txtBoxDrugsFactory.Text = dt_1.Rows[0]["drugsFactory"].ToString();
                this.txtBoxDrugsInPrice.Text = dt_1.Rows[0]["drugsInPrice"].ToString();
                this.txtBoxDrugsSalePrice.Text = dt_1.Rows[0]["drugsSalePrice"].ToString();
            }
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
                return; 
            }
        }
        private void Save()
        {
            try
            {
                string c1 = "";
                string c2 = "";
                string c3 = "";
                string c4 = "";
                string c5 = "";
                string c6 = "";
                string c7 = "";
                string c8 = "";
                string c9 = "";
                string c10 = "";
                DataTable dt1 = new DataTable();
                dt1 = op.getDataTable("select max(drugsId) from drugsInfo", "drugsInfo");
                if (dt1 != null && dt1.Rows.Count > 0)
                {
                    c10 = dt1.Rows[0][0].ToString();
                    c10 = Convert.ToString(Convert.ToInt16(c10) + 1);
                }

                c1 = txtBoxDrugsID.Text.Trim();
                c2 = txtBoxDrugsName.Text.Trim();
                c3 = txtBoxDrugsPY.Text.Trim();
                c4 = txtBoxDrugsSpec.Text.Trim();
                c5 = comboDrugsUnit.Text.Trim();
                c6 = txtBoxDrugsRegNum.Text.Trim();
                c7 = txtBoxDrugsFactory.Text.Trim();
                c8 = txtBoxDrugsInPrice.Text.Trim();
                c9 = txtBoxDrugsSalePrice.Text.Trim();

                if (txtBoxDrugsID.Text.Trim() == "")
                {

                    DataTable dt2 = new DataTable();
                    dt2 = op.getDataTable("select * from drugsInfo where drugsId ='" + c1 + "'", "drugsInfo");

                    if (txtBoxDrugsName.Text == "")
                    {
                        MessageBox.Show("药品名称不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxDrugsSpec.Text == "")
                    {
                        MessageBox.Show("药品规格不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (comboDrugsUnit.Text == "")
                    {
                        MessageBox.Show("计量单位不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxDrugsRegNum.Text == "")
                    {
                        MessageBox.Show("批准文号不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxDrugsFactory.Text == "")
                    {
                        MessageBox.Show("生产厂家不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxDrugsInPrice.Text == "")
                    {
                        MessageBox.Show("进货单价不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    string sql_a = "insert into drugsInfo(drugsName,drugsPY,drugsSpecification,drugsUnit,drugsRegNum,drugsFactory,drugsInPrice,drugsSalePrice) values ('" + c2 + "','" + c3 + "','" + c4 + "','" + c5 + "','" + c6 + "','" + c7 + "','" + c8 + "','" + c9 + "')";
                    op.ExecuteSql(sql_a);
                    MessageBox.Show("保存成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    txtBoxDrugsID.ReadOnly = true;
                    txtBoxDrugsName.ReadOnly = true;
                    txtBoxDrugsPY.ReadOnly = true;
                    txtBoxDrugsSpec.ReadOnly = true;
                    txtBoxDrugsRegNum.ReadOnly = true;
                    txtBoxDrugsFactory.ReadOnly = true;
                    txtBoxDrugsInPrice.ReadOnly = true;
                    txtBoxDrugsSalePrice.ReadOnly = true;

                    txtBoxDrugsID.Text = "";
                    txtBoxDrugsName.Text = "";
                    txtBoxDrugsPY.Text = "";
                    txtBoxDrugsSpec.Text = "";
                    comboDrugsUnit.Text = "";
                    txtBoxDrugsRegNum.Text = "";
                    txtBoxDrugsFactory.Text = "";
                    txtBoxDrugsInPrice.Text = "";
                    txtBoxDrugsSalePrice.Text = "";

                    DataTable dt = new DataTable();
                    string sql_b = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo order by drugsId";
                    dt = op.getDataTable(sql_b, "drugsInfo");
                    dataGridView1.DataSource = dt;
                    return;
                }

                string sql_1 = "update drugsInfo set drugsName='" + c2 + "',drugsPY='" + c3 + "',drugsSpecification='" + c4
                    + "',drugsUnit='" + c5 + "',drugsRegNum='" + c6 + "',drugsFactory='" + c7 + "',drugsInPrice='" + c8 + "',drugsSalePrice='" + c9 + "' where drugsId='" + c1 + "'";
                op.ExecuteSql(sql_1);
                MessageBox.Show("修改成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                txtBoxDrugsID.ReadOnly = true;
                txtBoxDrugsName.ReadOnly = true;
                txtBoxDrugsPY.ReadOnly = true;
                txtBoxDrugsSpec.ReadOnly = true;
                txtBoxDrugsRegNum.ReadOnly = true;
                txtBoxDrugsFactory.ReadOnly = true;
                txtBoxDrugsInPrice.ReadOnly = true;
                txtBoxDrugsSalePrice.ReadOnly = true;

                txtBoxDrugsID.Text = "";
                txtBoxDrugsName.Text = "";
                txtBoxDrugsPY.Text = "";
                txtBoxDrugsSpec.Text = "";
                comboDrugsUnit.Text = "";
                txtBoxDrugsRegNum.Text = "";
                txtBoxDrugsFactory.Text = "";
                txtBoxDrugsInPrice.Text = "";
                txtBoxDrugsSalePrice.Text = "";

                DataTable dt11 = new DataTable();
                string sql_2 = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo";
                dt11 = op.getDataTable(sql_2, "drugsInfo");
                dataGridView1.DataSource = dt11;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
             try
            {
                if (this.txtBoxDrugsID.Text == "")
                {
                    MessageBox.Show("请选择需要删除的内容！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (MessageBox.Show("确定需要删除用户[ " + this.txtBoxDrugsName.Text.Trim() + " ]吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    op.ExecuteSql("delete from  drugsInfo where drugsId='" + this.txtBoxDrugsID.Text.Trim() + "'");
                    DataTable dt = new DataTable();
                    string sql = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo order by drugsId";
                    dt = op.getDataTable(sql, "drugsInfo");
                    dataGridView1.DataSource = dt;
                    MessageBox.Show("删除成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void btnChange_Click_1(object sender, EventArgs e)
        {
            if (this.txtBoxDrugsID.Text == "")
            {
                MessageBox.Show("请选择需要修改的内容！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            this.txtBoxDrugsName.ReadOnly = false;
            this.txtBoxDrugsPY.ReadOnly = false;
            this.txtBoxDrugsSpec.ReadOnly = false;
            this.txtBoxDrugsRegNum.ReadOnly = false;
            this.txtBoxDrugsFactory.ReadOnly = false;
            this.txtBoxDrugsInPrice.ReadOnly = false;
            this.txtBoxDrugsSalePrice.ReadOnly = false;
            this.txtBoxDrugsName.Focus();
        }

       
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            this.txtBoxDrugsPY.Text = DrugsShopManagement.DBCLASS.Operator.GetSpellCode(this.txtBoxDrugsName.Text);    
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.txtBoxDrugsPY.Text = DrugsShopManagement.DBCLASS.Operator.GetSpellCode(this.txtBoxDrugsName.Text);   
        }

        

        private void FrmDrugsInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                Save();
            }
        }

        private void tb_DrugsSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo where drugsName like '%" + this.tetBoxDrugsSearch.Text.Trim() + "%'or drugsPY like '%" + this.tetBoxDrugsSearch.Text.ToUpper() + "%' order by drugsId";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
    }
}
